//
//  MSCreatProjectTableVC.m
//  SocietySale
//
//  Created by Mike on 16/11/17.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSCreatProjectTableVC.h"
#import "MSTradeConfirmContactsCell.h"
#import "MSSetTableViewCell.h"

@interface MSCreatProjectTableVC ()<UITextFieldDelegate>

@end

@implementation MSCreatProjectTableVC

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0<indexPath.row && indexPath.row<4) {
        NSString *cellId = @"cellId";
        MSSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[MSSetTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.textColor = kDarkGrayColor;
        cell.subLabel.textAlignment = NSTextAlignmentLeft;
        cell.subLabel.frame = CGRectMake(90, 0, SCREEN_WIDTH-112, cell.height);
        cell.subLabel.textColor = [UIColor blackColor];
        cell.data = self.dataArray[indexPath.row];
        return cell;
    }
    else{
        NSString *cellId = @"ContactsCell";
        MSTradeConfirmContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[MSTradeConfirmContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.inputField.delegate = self;
        cell.inputField.tag = indexPath.row;
        cell.textLabel.textColor = kDarkGrayColor;
        cell.data = self.dataArray[indexPath.row];

        if (indexPath.row == 4) {
            cell.horizonLineView.hidden = YES;
            cell.unitLabel.hidden = NO;
            cell.inputField.keyboardType = UIKeyboardTypeNumberPad;
            cell.inputField.width = SCREENWIDTH - 125;
        }
        else{
            cell.horizonLineView.hidden = NO;
            cell.unitLabel.hidden = YES;
            cell.inputField.keyboardType = UIKeyboardTypeDefault;
            cell.inputField.width = SCREENWIDTH - 100;
            [cell.inputField addTarget:self action:@selector(customTextFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
        }
        return cell;
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    DOKeyValue *data = self.dataArray[textField.tag];
    data.value = textField.text;
    return YES;
}

- (void)customTextFieldEditingChanged:(UITextField *)textField{
    if (textField.tag == 0) {
        NSInteger textLength = (textField.text.length < 20) ? textField.text.length : 20;
        textField.text = [textField.text substringWithRange:NSMakeRange(0, textLength)];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENFRAME.size.width, 10)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [bgView addSubview:horizonLineView];
    return bgView;
}

@end
